package com.example.filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 设置请求和响应的编码为UTF-8
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        
        System.out.println("EncodingFilter已执行：已将编码设置为UTF-8。");
        
        // 放行，让请求继续
        chain.doFilter(request, response);
    }
    
    // init 和 destroy 方法可以保留为空
    @Override public void init(FilterConfig fConfig) throws ServletException {}
    @Override public void destroy() {}
}
